<template>
    <div class="wrapper">
        <top-header title="产品管理"></top-header>
        <list class="item-container">
            <refresh @refresh="loadingDown()"></refresh>
            <cell class="border-cell"></cell>
            <cell v-for="(demo,key) in goodsList"
                class="cell"
                :key="key">
                <div class="content" @click="select(demo)">
                    <image class="content_img" v-if="demo.pics" :src="demo.pics"></image>
                    <image class="content_img" v-else src="bmlocal://assets/acw/car.jpg"></image>
                    <div>
                        <text class="content_first">{{demo.name}}</text>
                        <text class="content_discription">{{demo.description}}</text>
                        <div class="content_third">
                            <text class="content_third_price">售价：{{demo.goodprice}}</text>
                            <text class="content_third_price">状态：{{demo.isshow ? "上架" :"下架"}}</text>
                        </div>
                        <div class="content_four">
                            <text class="content_four_text" @click="shelfGoods(demo.id)" >上架</text>
                            <text class="content_four_text" @click="obtainedGoods(demo.id)" >下架</text>
                            <text class="content_four_text" @click="toUpdate(demo.id)">编辑</text>
                            <text class="content_four_text" @click="deleteGoods(demo.id)">删除</text>
                        </div>
                    </div>
                </div>
                
            </cell>
            <cell class="cell" v-if="goodsList.length <= 0">
                <div class="no-order">
                    <!-- <image src="bmlocal://assets/zst/wudingdan.png" class="noOrderImg"></image> -->
                    <text>暂无产品</text>
                </div>
            </cell>
            <loading class="loading" @loading="onloading()" :display="showLoading">
                <loading-indicator class="comm-indicator" v-if="!lastPage"></loading-indicator>
                <text class="comm-indicator-text" >{{loadingText}}</text>
            </loading>
        </list>
    </div>
</template>
<script>
import { WxcButton } from 'weex-ui';
import header from '../../common/header'
export default {
    components: { WxcButton, 
        'top-header': header, 
    },
    data () {
        return {
            token: '',
            goodsList: [],
            nextClick: true,
            showLoading: 'hide',
            btnStyle: {
                'background-color': '#1E90FF',
            },
            commodityId: [],
            searchParam: {
                "limit": 10,
                "order": "asc",
                "page": 1,
                "sort": "id",
                "storeid": '',
                "type": '2',
            },
            lastPage:false,//是否是最后一页
            loadingText:'加载中 ...',
        }
    },
    created () {
        let user = this.$storage.getSync('UserInfo')
        this.token = user.token
        let storeInfo = this.$storage.getSync('StoreInfo')
        // this.$notice.toast({message: storeInfo})
        this.searchParam.storeid = storeInfo.id
        this.loadingDown()
    },
    eros: {
        beforeBackAppear() {
            this.loadingDown()
        }
    },
    methods: {
        // 返回
        back () {
            this.$router.back();
        },
        callPhone (phone) {
            this.$coms.call(phone)
        },
        loadingDown() {
          this.goodsList = []
          this.searchParam.page = 1
          this.getGoodsList(this.searchParam)
        },
        onloading() {
            this.showLoading = 'show';
            this.$notice.loading.show();
            this.searchParam.page++
            if (!this.lastPage) {
               this.getGoodsList(this.searchParam)
            }
            setTimeout(() => {
                this.$notice.loading.hide();
                this.showLoading = 'hide'
            }, 600)
        },
        // 跳转订单
        toOrder () {
            this.$router.open({
                name: 'acw.service.maintennace.order',
                type: 'PUSH',
                params: this.sellCarParam
            });
            return
        },
        // 选这商品
        select (item) {
            if (this.commodityId.indexOf(item.id) == -1) {
                item.isSelect = true
                this.commodityId.push(item.id)
            } else {
                item.isSelect = false
                this.commodityId.splice(this.commodityId.indexOf(item.id), 1)
            }
        },
        // 查询商品列表
        getGoodsList (param) {
            this.$notice.loading.show();
            this.$fetch({
                method: 'POST',    // 大写
                name: 'goods.list', //当前是在apis中配置的别名，你也可以直接绝对路径请求 如：url:http://xx.xx.com/xxx/xxx
                data: param
            }).then(resData => {
                // 成功回调
                this.isShowLoad = false;
                console.log(resData)
                // this.$notice.toast({ message: resData.data})
                if(resData && resData.errno == 0){
                    let list = resData.data.items
                    for (let i = 0, len = list.length; i < len; i++) {
                        this.goodsList.push(list[i])
                    }
                }
                if(resData.data && this.goodsList.length >= resData.data.total){
                    this.lastPage = true;
                    this.loadingText = '没有更多'
                }
                this.$notice.loading.hide();
            }, error => {
                // 错误回调
                this.isShowLoad = false;
                console.log(error)
            })
        },
        // 删除商品
        deleteGoods (id) {
            this.$fetch({
                method: 'POST',    // 大写
                name: 'goods.delete', //当前是在apis中配置的别名，你也可以直接绝对路径请求 如：url:http://xx.xx.com/xxx/xxx
                data: {'id': id}
            }).then(resData => {
                // 成功回调
                this.isShowLoad = false;
                console.log(resData)
                // this.$notice.toast({ message: resData.data})
                if(resData && resData.errno == 0){
                    this.$notice.toast({ message: '删除成功'})
                    this.loadingDown()
                } else {
                    this.$notice.toast({ message: '删除失败'})
                }
            }, error => {
                // 错误回调
                this.isShowLoad = false;
                console.log(error)
            })
        },
         // 上架商品
        shelfGoods (id) {
            this.$fetch({
                method: 'POST',    // 大写
                name: 'goods.shelf', //当前是在apis中配置的别名，你也可以直接绝对路径请求 如：url:http://xx.xx.com/xxx/xxx
                data: {'id': id}
            }).then(resData => {
                // 成功回调
                this.isShowLoad = false;
                console.log(resData)
                this.$notice.toast({ message: resData.data})
                if(resData && resData.errno == 0){
                    this.$notice.toast({ message: '上架成功'})
                    this.loadingDown()
                } else {
                    this.$notice.toast({ message: '上架失败'})
                }
            }, error => {
                // 错误回调
                this.isShowLoad = false;
                console.log(error)
            })
        },
         // 下架商品
        obtainedGoods (id) {
            this.$fetch({
                method: 'POST',    // 大写
                name: 'goods.obtained', //当前是在apis中配置的别名，你也可以直接绝对路径请求 如：url:http://xx.xx.com/xxx/xxx
                data: {'id': id}
            }).then(resData => {
                // 成功回调
                this.isShowLoad = false;
                console.log(resData)
                this.$notice.toast({ message: resData})
                if(resData && resData.errno == 0){
                    this.$notice.toast({ message: '下架成功'})
                    this.loadingDown()
                } else {
                    this.$notice.toast({ message: '下架失败'})
                }
            }, error => {
                // 错误回调
                this.isShowLoad = false;
                console.log(error)
            })
        },
        // 修改商品
        toUpdate (id) {
            this.$router.open({
                name: 'acw.my.merchant.updatecommod',
                type: 'PUSH',
                params: id
            });
            return
        },
    }
}
</script>
<style scoped>
@import 'src/js/css/base.scss';
.iconfont {
    font-family: iconfont;
}
.wrapper{
    background-color: #fff;
}
.item-container {
    width: 750px;
    background-color: #f2f3f4;
    padding: 20px;
}
.cell{
    background-color: #f2f3f4;
    width: 710px;
}
.border-cell {
    background-color: #f2f3f4;
    width: 750px;
    height: 24px;
    align-items: center;
    justify-content: center;
    border-bottom-width: 1px;
    border-style: solid;
    border-color: #e0e0e0;
}
.no-order {
    justify-content: center;
    align-items: center;
    height: 1034px;
    width: 750px;
}
.content {
    flex-direction: row;
    border-style: solid;
    border-bottom-width:1px;
    border-color: #e0e0e0;
    background-color: #fff;
    padding: 20px;
    margin-bottom: 20px;
    border-radius: 10px;
}
.content_img{
    width: 140px;
    height: 140px;
    margin-right: 10px;
}
.content_four{
    width: 520px;
    flex-direction: row;
    justify-content: space-around;
    padding-top: 10px;
}
.content_first, 
.content_four_text{
    font-size: 30px;
    height: 50px;
    line-height: 50px;
}
.content_discription{
    font-size: 30px;
}
.content_four_text{
    color: #1E90FF;
}
.content_third{
    flex-direction: row;
}
.content_four_phone{
    width: 50px;
    height: 50px;
    line-height: 50px;
    color: #1E90FF;
    margin-left: 300px;
}
.content_third{
    flex-direction: row;
}
.content_third_price{
    font-size: 30px;
    color: #1E90FF;
    margin-left: 20px;
    margin-right: 40px;
}
</style>
